import _ from 'lodash';
import { DialogInstance, DialogPlugin } from 'tdesign-vue-next';

import { SUCCESS_CODE } from '@/constants';

export const isDevelopment = () => {
  return import.meta.env.MODE || 'development';
};

// 获取子级数据
export const getRegionChildrenData = async (code?: string) => {
  let list: any = [];

  try {
    console.log('code', code);
    // const data = code ? { code } : {};
    // const res = await openApi.region(data);
    const res: any = {};

    if (res.code === SUCCESS_CODE) {
      list = res.data?.children;
    }
  } catch (error) {
    console.log('error', error);
  }

  return list;
};

// 动态加载子级数据
export const cascaderLoad = (node: any): any => {
  // eslint-disable-next-line no-async-promise-executor
  return new Promise(async (resolve) => {
    try {
      const list = await getRegionChildrenData(node.value);
      resolve(list);
    } catch (error) {
      console.log('error', error);
    }
  });
};

// 获取父级数据
export const getRegionParentsData = async (code?: string) => {
  let list: any[] = [];
  console.log('code', code);
  try {
    // const res = await openApi.region({ code, with_parent: true });
    const res: any = {};

    if (res.code === SUCCESS_CODE) {
      const arr = [res.data?.data, ...(res.data?.parent_level || [])];
      list = arr.map((item: any) => item.code);
      list.push('');
    }
  } catch (error) {
    console.log('error', error);
  }

  return list.reverse();
};

// 弹框
export function confirmData(data: any): Promise<DialogInstance | false> {
  return new Promise((resolve: any) => {
    const confirmDialog = DialogPlugin.confirm({
      header: data.title,
      body: data.content,
      confirmBtn: data.confirm || '确定',
      cancelBtn: data.cancel || '取消',
      onConfirm: () => {
        resolve(confirmDialog);
      },
      onClose: () => {
        confirmDialog.hide();
        resolve(false);
      },
    });
  });
}

// 提示
export function alertData(data: any) {
  return new Promise((resolve: any) => {
    const confirmDialog = DialogPlugin.alert({
      header: data.header,
      body: data.body,
      confirmBtn: data.confirm || '确定',
      onConfirm: () => {
        confirmDialog.destroy();
        resolve(true);
      },
    });
  });
}

// 判断值是否为空值
export const isEmptyValue = (value: any) => {
  return _.isNil(value) || value === '' || Number.isNaN(value);
};

// 设置详情的kv组件的数据
export const setKvViewData = (list: any[], data: any, options?: any) => {
  const { attr, blankStr } = options || {};
  if (!list || !list?.length || !data) return [];
  list.forEach((item: any) => {
    item[attr || 'value'] = item.key && !isEmptyValue(data[item.key]) ? data[item.key] : blankStr || '';
  });
  return list;
};

// 过滤kv列表数据
export const filterKvViewList = (arr: any[], list: (number | string)[]) => {
  return arr.filter((item) => list.includes(item.key));
};

// 清空kv所有数据
export const clearKvViewList = (list: any[]) => {
  list.forEach((item) => {
    item.value = '';
  });
};

export const addCommas = (num: number) => (num || 0).toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1'");

// 获取图片数据
export const getImageInfo = (url: string): Promise<HTMLImageElement | false> => {
  return new Promise((resolve: any) => {
    const image = new Image();
    image.src = url;

    image.onload = () => {
      resolve(image);
    };

    image.onerror = () => {
      resolve(false);
    };
  });
};
